package com.ticket.sass.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.mapper.CinemaConfigMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.CinemaConfigEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-06-07
 */
@Service
public class CinemaConfigServiceImpl extends ServiceImpl<CinemaConfigMapper, CinemaConfigEntity> implements BaseService<CinemaConfigEntity> {
    /**
     * 设置二维码
     * @param key 键值
     * @param value 二维码
     */
    @Transactional
    public void setQrCode(String key, String value) {
        QueryWrapper<CinemaConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("key", key);
        
        value = StrUtil.isEmpty(value) ? "" : value;
        
        CinemaConfigEntity config = this.getOne(queryWrapper);
        if (config == null) {
            config = new CinemaConfigEntity();
            config.setKey(key);
            config.setValue(value);
            this.save(config);
        } else {
            config.setValue(value);
            this.updateById(config);
        }
    }
    
    /**
     * 获取二维码
     * @param key 键值
     * @return 二维码
     */
    public String getQrCode(String key) {
        QueryWrapper<CinemaConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper
            .eq("key", key);
        
        CinemaConfigEntity config = this.getOne(queryWrapper);
        return config != null ? config.getValue() : "";
    }
}
